package com.jk.controller;

import com.jk.ReturnMsgUtils;
import com.jk.api.PayServiceApi;
import com.jk.dao.PayDao;
import com.jk.po.OrderPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Map;

@RestController
public class PayController implements PayServiceApi {

    @Autowired
    private PayDao payDao;
    @Autowired
    private MongoTemplate mongoTemplate;


    //付款接口
    @Override
    public Map payMoney(String id, Integer totalprice) {
        //由于账户金额是bigDecimal(专门用于金额计算)，因此我们需要将付款金额也转换成BigDecimal
        BigDecimal bb = BigDecimal.valueOf(totalprice);

        //执行功能
        payDao.payMoney(id,totalprice);

        return ReturnMsgUtils.returnMsg(200,"扣款成功",null);
    }


    /**
     * @param id
     * @author 段卓帆
     * @date 2021/7/8 15:20
     * @return
     */
    //付完款之后修改订单状态为已支付
    @Override
    public void upStatusById(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update=new Update();
        update.set("orderstatus",1);
        mongoTemplate.upsert(query, update, OrderPO.class);
    }


}
